package model;

import java.io.BufferedWriter;
import java.io.FileWriter;

import org.json.JSONArray;
import org.json.JSONObject;

import util.TwitterSearcher;

public class TwitterAnalyzer {
	
	public TwitterAnalyzer() {
		
	}
	
	public void analyzeTerm(String term) throws Exception {
		Polarity tweetPolarity = new Polarity("bases/twitter");
		
		JSONArray tweets = TwitterSearcher.getTweetsByTextAndNumber(term, 100);
      	JSONObject tweet;
      
      	FileWriter fstream = new FileWriter(term + ".txt");
		
		BufferedWriter out = new BufferedWriter(fstream);

		for(int i = 0; i < tweets.length(); i++) {  
			tweet = tweets.getJSONObject(i);
			
			out.write(tweetPolarity.check(tweet.getString("text")) + " - " + tweet.getString("text"));
			
			if(i != tweets.length() - 1) {
				out.write("\n");
			}
		}

		out.flush();
		out.close();
		
		System.out.println("Arquivo " + term + ".txt criado");
	}

}
